// pages/cart.js
const api = require('../../../utils/api.js')
const config = require("../../../utils/config.js")
const w = require("../../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemList: [],
    cartTotal: {},
    token:"",
    id:"",
    product_id:""

  },

  // 商品加减
  countOperate: function (e) {
   
    var i = e.currentTarget.dataset.index
    var operator = e.target.dataset.operator
    var num = this.data.itemList[i].number
    var count = 'itemList[' + i + '].number'
    var status = 'itemList[' + i + '].minusStatus'
    if (operator == 'add') {
      num++
      this.setData({
        [count]: num,
        [status]: num > 0 ? false : true
      })
    } else if (operator == 'minus') {
      num--
      this.setData({
        [count]: num,
        [status]: num > 0 ? false : true
      })
      if (num == 0) {
        wx.showToast({
          title: '最少选择一件',
          icon: "none"
        })
        num = 1;
      }
    }
    this.Update(e,num)
  },

  // 删除购物车商品
  cancelItem: function(e) {
    var i = e.currentTarget.dataset.index
    var id = this.data.itemList[i].product_id.toString();
    var that = this
    wx.request({
      url: api.api.cartDelete,
      method: 'POST',
      header: {
        "X-Nideshop-Token": that.data.token
      },
      data: {
        productIds: id
      },
      success: function (res) {
        // console.log('del', res)
        
        that.data.itemList.splice(i, 1)
        that.setData({
          itemList: that.data.itemList
        })
        that.getshopp();
      }
    })
    
  },

  // checkbox事件
  checkedChange: function (e) {
    var i = e.currentTarget.dataset.index
    var str = `itemList[${i}].checked`
    var data = !this.data.itemList[i].checked
    this.setData({
      [str]: data
    })
  },

  // 全选事件
  selectAll: function (e) {
    for (var i = 0; i < this.data.itemList.length; i++) {
      this.data.itemList[i].checked = e.detail.value.length == 1 ? true : false
    }
    this.setData({
      itemList: this.data.itemList
    })
  },

  // 结算
  pay: function (e) {

  },
  // 获取购物车
  getshopp(){
    var that = this;
    // var that = this
    wx.getStorage({
      key: 'useof',
      success: function (res) {
        // console.log(res)
        that.setData({
          token: res.data.token
        })
        wx.request({
          url: api.api.cart,
          method: "GET",
          header: {
            "X-Nideshop-Token": that.data.token
          },
          success: function (res) {
            console.log(res)
            res.data.data.cartList.forEach((item, index) => {
              // console.log(item)
              var i = item['minusStatus']

              i = item.number > 1 ? 'true' : 'false'
              // console.log(i)
            })
            that.setData({
              itemList: res.data.data.cartList,
              cartTotal: res.data.data.cartTotal,
              id:res.data.data.cartList,
              product_id:res.data.data.cartList
            })
            // console.log('id', res)
          
          }
        })
      },
    })
  },
  // 更新
  Update(e, num){
    var that=this;
    // console.log(e);
    // console.log(num)
    var Y = e.currentTarget.dataset.index;
    // console.log(this.data.id);
    w.request(api.api.cartUpdate,"POST",
    {
      id: that.data.id[Y].id,
      goodsId: that.data.id[Y].goods_id,
      number: num,
      productId: that.data.id[Y].product_id
    },
     function(res){
        console.log(res)
       that.getshopp();
     }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getshopp();
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})